from paste.deploy.config import ConfigMiddleware
import model
import webob
import controllers
import controllers.people
import selector
from beaker.middleware import SessionMiddleware
from lib import base
from sqlalchemy import engine_from_config
from paste.urlparser import PkgResourcesParser
from paste.cascade import Cascade


def make_app(
    global_conf,
    greeting,
    **kw):
    app = selector.Selector(consume_path=False)
    controllers.routing(app)
    controllers.people.routing(app)
    conf = global_conf.copy()
    conf.update(kw)
    engine = engine_from_config(conf, 'sqlalchemy.default.')
    model.Session.configure(bind=engine)
    app = base.Middleware(app, conf)
    app = SessionMiddleware(app, type="dbm", 
                            key='mysession', secret='randomstring',
                            data_dir=conf['session.data_dir'])
    app = ConfigMiddleware(app, conf)
    static = PkgResourcesParser('onsocial', 'onsocial/static')
    app = Cascade([static, app])
    return app

